package com.example.cofeed.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.cofeed.R;
import com.example.cofeed.adapter.SelectCityAdapter;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.constant.IntentKeyConstant;
import com.example.cofeed.constant.SputilsConstant;
import com.example.cofeed.entity.City;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectCityActivity extends BaseActivity {

    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private String catalogId;
    private SelectCityAdapter selectCityAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_city;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        setMainTitle("选择城市");

        catalogId = getIntent().getStringExtra(IntentKeyConstant.CATALOG_ID);

        selectCityAdapter = new SelectCityAdapter();
        mRvList.setLayoutManager(new LinearLayoutManager(this));
        mRvList.setAdapter(selectCityAdapter);
        selectCityAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                final City city = (City) adapter.getData().get(position);
                selectCityAdapter.setSelect(city);
                ApiService.cartAdd(this, catalogId, city.cityid, city.selected, new DialogCallback<LzyResponse<Object>>() {
                    @Override
                    public void onSuccess(Response<LzyResponse<Object>> response) {

                    }
                });
            }
        });

        ApiService.subscribeCity(this, getIntent().getIntExtra(SputilsConstant.TYPE, 1), catalogId, new DialogCallback<LzyResponse<List<City>>>(this) {
            @Override
            public void onSuccess(Response<LzyResponse<List<City>>> response) {
                selectCityAdapter.setNewData(response.body().data);
            }
        });

        mRefreshLayout.setEnableLoadMore(false);
        mRefreshLayout.setEnableRefresh(false);

    }

    @OnClick({R.id.ll_dingyue, R.id.ll_cart})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_dingyue:
                finish();
                break;
            case R.id.ll_cart:
                Intent intent = new Intent(this, CartActivity.class);
                intent.putExtra(IntentKeyConstant.TYPE, 1);
                startActivity(intent);
                finish();
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
